package com.xunua.NaoZiKongBai.LoginActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.TextKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.wingsofts.threedlayout.ThreeDLayout;
import com.xunua.NaoZiKongBai.Base.BaseActivity;
import com.xunua.NaoZiKongBai.Bean.EventBus.LoginEtEvent;
import com.xunua.NaoZiKongBai.MainActivity.MainActivity;
import com.xunua.NaoZiKongBai.R;
import com.xunua.NaoZiKongBai.RegisterActivity.RegisterActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity<LoginPresenter, LoginContract.VP> implements View.OnClickListener, View.OnFocusChangeListener {

    @BindView(R.id.login_statusBar)
    View loginStatusBar;
    @BindView(R.id.login_et_name)
    EditText loginEtName;
    @BindView(R.id.login_et_pwd)
    EditText loginEtPwd;
    @BindView(R.id.login_name_clear)
    ImageView loginNameClear;
    @BindView(R.id.login_rel_name)
    RelativeLayout loginRelName;
    @BindView(R.id.login_pwd_clear)
    ImageView loginPwdClear;
    @BindView(R.id.login_rel_pwd)
    RelativeLayout loginRelPwd;
    @BindView(R.id.login_img_icon)
    ImageView loginImgIcon;
    @BindView(R.id.login_threedLayout)
    ThreeDLayout loginThreedLayout;
    @BindView(R.id.login_rel_ets)
    RelativeLayout loginRelEts;
    @BindView(R.id.login_btn_login)
    Button loginBtnLogin;
    @BindView(R.id.login_forgetpwd)
    TextView loginForgetpwd;
    @BindView(R.id.login_register)
    TextView loginRegister;


    private int REGISTER_REQUESTCODE=10087;
    @Override
    public LoginContract.VP getContract() {
        return new LoginContract.VP() {
            @Override
            public void requestLogin(String name, String pwd, Context context) {
                mPresenter.getContract().requestLogin(name, pwd,context);
                myApplication.startRequest=false;//暂停验证账号状态
            }

            @Override
            public void responseLoginResult(boolean loginStatusResult,boolean isban) {
                hideLoadingDialogBase();
                if (loginStatusResult && !isban) {//登录成功且账号未被封禁
                    myApplication.startRequest=true;//继续验证账号状态
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }else {
                    if (isban) {
                        Toast.makeText(LoginActivity.this, "账号已被封禁", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };
    }

    @Override
    public void setContentViewId() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }
    @Override
    public void init() {
        SharedPreferences sp = getSharedPreferences("userlogin", MODE_PRIVATE);
        String name = sp.getString("name", "");
        loginEtName.setText(name);
        loginBtnLogin.setOnClickListener(this);
        //忘记密码
        loginForgetpwd.setOnClickListener(this);
        //注册账号
        loginRegister.setOnClickListener(this);

        loginThreedLayout.setTouchable(true);
        loginThreedLayout.setTouchMode(ThreeDLayout.MODE_BOTH_X_Y);
        loginEtName.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.NONE, true) {
            @Override
            public int getInputType() {
                return InputType.TYPE_CLASS_PHONE;
            }
        });
        loginNameClear.setOnClickListener(this);
        loginPwdClear.setOnClickListener(this);
        loginEtName.setOnFocusChangeListener(this);
        loginEtPwd.setOnFocusChangeListener(this);

        //文字监听  在文字字数为0时隐藏清空按钮
        loginEtName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                EventBus.getDefault().post(new LoginEtEvent(true));
                if (s.length() != 0) {
                    loginNameClear.setVisibility(View.VISIBLE);
                } else {
                    loginNameClear.setVisibility(View.GONE);
                }
            }
        });
        loginEtPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                EventBus.getDefault().post(new LoginEtEvent(true));
                if (s.length() != 0) {
                    loginPwdClear.setVisibility(View.VISIBLE);
                } else {
                    loginPwdClear.setVisibility(View.GONE);
                }
            }
        });
    }


    @Override
    public LoginPresenter getPresenterInstance() {
        return new LoginPresenter();
    }

    @Override
    public boolean IskeyboardEnable() {
        return false;
    }

    @Override
    public View getStatusBar() {
        return loginStatusBar;
    }

    @Override
    protected void onPause() {
        super.onPause();
        loginEtName.clearFocus();
        loginEtPwd.clearFocus();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_name_clear:
                loginEtName.setText("");
                break;
            case R.id.login_pwd_clear:
                loginEtPwd.setText("");
                break;
            case R.id.login_register://注册账号
                startActivityForResult(new Intent(this, RegisterActivity.class),REGISTER_REQUESTCODE);
                break;
            case R.id.login_forgetpwd://忘记密码  没写
                Toast.makeText(this, "敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.login_btn_login://账号登录
                showLoadingDialogBase(this,"正在登录...",false);
                String name = loginEtName.getText().toString().trim();
                String pwd=loginEtPwd.getText().toString().trim();
                getContract().requestLogin(name,pwd,this);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REGISTER_REQUESTCODE) {//注册完毕
            if (resultCode==RESULT_OK){
                Bundle extras = data.getExtras();
                String phone = extras.getString("phone");
                String pwd = extras.getString("pwd");
                loginEtName.setText(phone);
                loginEtPwd.setText(pwd);
                loginBtnLogin.performClick();
            }
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {//实现输入框的监听，如果监听到触控 那么就隐藏hint内容，并且第一次点击的时候会判断et的内容长度，然后判断是否显示清空按钮  配合EditText的文字变化监听
        switch (v.getId()) {
            case R.id.login_et_name:
                if (hasFocus) {
                    if (loginEtName.getText().length() != 0) {
                        loginNameClear.setVisibility(View.VISIBLE);
                    } else {
                        loginNameClear.setVisibility(View.GONE);
                    }
                    loginEtName.setHint("");
                } else {
                    loginNameClear.setVisibility(View.GONE);
                    loginEtName.setHint("账号");
                }
                break;
            case R.id.login_et_pwd:
                if (hasFocus) {
                    if (loginEtPwd.getText().length() != 0) {
                        loginPwdClear.setVisibility(View.VISIBLE);
                    } else {
                        loginPwdClear.setVisibility(View.GONE);
                    }
                    loginEtPwd.setHint("");
                } else {
                    loginPwdClear.setVisibility(View.GONE);
                    loginEtPwd.setHint("输入密码");
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (loginEtName.getText().toString().trim().length() > 0 && loginEtPwd.getText().toString().trim().length() > 0) {
            loginBtnLogin.setBackgroundResource(R.drawable.login_btn_click);
            loginBtnLogin.setClickable(true);
        } else {
            loginBtnLogin.setBackgroundResource(R.drawable.login_btn_click_no);
            loginBtnLogin.setClickable(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)    //用来处理登录按钮是否可点击
    public void EtChangeEvent(LoginEtEvent event) {
        if (loginEtName.getText().toString().trim().length() > 0 && loginEtPwd.getText().toString().trim().length() > 0) {
            loginBtnLogin.setBackgroundResource(R.drawable.login_btn_click);
            loginBtnLogin.setClickable(true);
        } else {
            loginBtnLogin.setBackgroundResource(R.drawable.login_btn_click_no);
            loginBtnLogin.setClickable(false);
        }
    }
}
